<script setup>
import apis from "../request/apis.js";
import {getCurrentInstance, ref} from "vue";
import moment from "moment";


const {proxy} = getCurrentInstance();

const data = ref({
  medicalRecordNo: null,
  startDate: new Date(moment().add(-30, 'days').format("YYYY-MM-DD")),
  endDate: new Date(moment().format("YYYY-MM-DD"))
});


const tableData = ref([]);

const columns = ref([{
  title: '序号',
  key: 'sortNo',
  type: 'index'
}, {
  title: '结算状态',
  key: 'settleStatusName',
}, {
  title: '结算流水号',
  key: 'serialNo',
}, {
  title: '来源',
  key: 'sourceName',
}, {
  title: '票据号',
  key: 'invoiceNo',
  slot: 'invoiceNo',
}, {
  title: '费用类型',
  key: 'costCategoryName',
}, {
  title: '结算卡号',
  key: 'residentCardNo',
}, {
  title: '总金额',
  key: 'settleAmount',
}, {
  title: '结算金额',
  key: 'settleOriginAmount',
}, {
  title: '记账金额',
  key: 'accountingAmount',
}, {
  title: '舍入金额',
  key: 'roundingAmount',
}, {
  title: '预交金抵扣金额',
  key: 'fundBillAmount',
}, {
  title: '收费时间',
  key: 'settleTime',
}, {
  title: '收费员',
  key: 'settlorName',
}, {
  title: '备注',
  key: 'remark',
}, {
  title: '操作',
  slot: 'action',
},
]);

const querySettleRecord = async () => {

  if (!data.value.medicalRecordNo) {
    proxy.$Message.info("请输入病历号");
    return;
  }
  //查询患者ID
  const patientInfo = await apis.queryResidentOrgRegistInfo(data.value.medicalRecordNo);
  if (!patientInfo) {
    proxy.$Message.info("获取患者信息失败");
    return;
  }

  const result = await apis.querySettleRecord({
    patientId: patientInfo.patientId,
    startDate: moment(data.value.startDate).format("YYYY-MM-DD"),
    endDate: moment(data.value.endDate).format("YYYY-MM-DD"),
  });

  tableData.value = result;
}

const billId = ref();
const reOpen = async (row) => {
//settleBillId
  const bill = await apis.getBillBySettleBillId(row.settleBillId);
  billId.value = bill.id;
}

const outHospitalRevokeFlag = ref(false);

const outHospitalRevokeFunc = async () => {
  if (!billId.value) {
    proxy.$Message.info("请输入票据号");
    return
  }
  await apis.reOpenInvoice(billId.value);
}
</script>

<template>
  <Layout>
    <Content class="layout-content">
      <Row>
        <Col span="24">

          <Space direction="vertical" size="large" type="flex">

            <Row>

              <Col span="2">

                <Input placeholder="请输入患者病历号" v-model="data.medicalRecordNo"></Input>

              </Col>
              &nbsp; &nbsp; &nbsp;
              <Col span="3">

                <DatePicker type="date" v-model="data.startDate" placeholder="Select date" style="width: 200px"/>

              </Col>
              &nbsp; &nbsp; &nbsp;

              <Col span="3">

                <DatePicker type="date" v-model="data.endDate" placeholder="Select date" style="width: 200px"/>

              </Col>
              &nbsp; &nbsp; &nbsp;

              <Col span="10">

                <Button type="primary" @click="querySettleRecord">查询</Button>

              </Col>
            </Row>
            <Row>
              <Col span="24">
                <Table height="400" stripe border highlight-row :columns="columns" :data="tableData">

                  <template #invoiceNo="{ row, index }">
                    {{ row.settleInvoice.invoiceNumber }}
                  </template>

                  <template #action="{ row, index }">
                    <Space wrap>

                      <Button type="primary" v-if="!row.settleInvoice?.invoiceNumber" @click="reOpen(row)">
                        重开
                      </Button>

                    </Space>
                  </template>

                </Table>
              </Col>
            </Row>
            <Row>
              <Col span="24">
                入参：
                <Input v-model="billId" show-word-limit type="textarea"
                       :rows="10"
                       placeholder="请输入billId"/>

                <Button type="primary" @click="outHospitalRevokeFlag=true">
                  确认
                </Button>
                <Modal
                    v-model="outHospitalRevokeFlag"
                    title="医保交易"
                    @on-ok="outHospitalRevokeFunc"
                    @on-cancel="outHospitalRevokeFlag=false">
                  <p>确认重开票据吗？</p>
                </Modal>
              </Col>
            </Row>

          </Space>

        </Col>

      </Row>
    </Content>
  </Layout>

</template>

<style scoped>

</style>